package com.czk.yygh.cmn.controller;

import com.czk.model.cmn.Dict;
import com.czk.yygh.cmn.service.DictService;
import com.czk.yygh.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/7 15:30
 */
@Api("字典设置管理")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Resource
    private DictService dictService;

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    @GetMapping("/exportData")
    @ApiOperation(value = "导出数据")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDict(response);
    }

    @PostMapping("/importData")
    @ApiOperation(value = "导入数据")

    public Result importDict(MultipartFile multipartFile) {
        dictService.importDict(multipartFile);
        return Result.ok();
    }

    @ApiOperation("通过dictCode和value获取名称")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value) {
        return dictService.getDictName(dictCode, value);
    }

    @ApiOperation("通过value获取名称")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable("value") String value) {
        return dictService.getDictName("", value);
    }

    @ApiOperation(value = "根据dictCode查询下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
